﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using WmsService.BusinessLogic;
using WmsService.Derived;
using WmsService.Reports;
using WmsService.Tacking;
using WmsService.Util;

namespace WmsService
{
    public partial class Service
    {
        #region Tacking

        // ReSharper disable MemberCanBeMadeStatic.Local
        private IStockTackingLogic StockTackingLogicInstance
        // ReSharper restore MemberCanBeMadeStatic.Local
        {
            get { return LogicFactoryCreator.CreateAbstractFactory().CreateStockTackingLogic(); }
        }

        public string GetStockTackingId()
        {
            return StockTackingLogicInstance.GetStockTackingId();
        }

        public StockTackingWrap GetStockTackingWrap(string stockTackingNo)
        {
            return StockTackingLogicInstance.GetStockTackingWrap(stockTackingNo);
        }

        public void SaveStockTackingWrap(StockTackingWrap stockTackingWrap)
        {
            StockTackingLogicInstance.SaveStockTackingWrap(stockTackingWrap);
        }

        public void AffirmStockTacking(string stockTackingNo)
        {
            StockTackingLogicInstance.AffirmStockTacking(stockTackingNo);
        }

        public List<StockTacking> QueryStockTackingList(string condition)
        {
            return StockTackingLogicInstance.QueryStockTackingList(condition);
        }

        public List<ExtendStockTackingDtl> QueryStockTackingDtlList(string condition)
        {
            return StockTackingLogicInstance.QueryStockTackingDtlList(condition);
        }
      
        public List<StockTacking> GetStockTackingList(string condition)
        {
            using (var ctx = Factory.ContextFactory.Create())
            {
                var list = ctx.ExecuteStoreQuery<StockTacking>(" select * from dbo.StockTacking where 1=1 " + condition);
                return list.ToList();
            }
        }

      
        public List<StockTackingDtl> GetStockTackingDtlList(string condition)
        {
            using (var ctx = Factory.ContextFactory.Create())
            {
                var list = ctx.ExecuteStoreQuery<StockTackingDtl>(" select * from dbo.StockTackingDtl where 1=1 " + condition);
                return list.ToList();
            }
        }

        public void DeleteStockTackingDtlList(List<StockTackingDtl> list)
        {
            this.SecurityValidate();
            using (var ctx = Factory.ContextFactory.Create())
            {
                foreach (var stockTackingDtl in list)
                {
                    if (stockTackingDtl.WcfEntityState != EntityState.Added)
                    {
                        ctx.AddToStockTackingDtl(stockTackingDtl);
                        ctx.ObjectStateManager.ChangeObjectState(stockTackingDtl, EntityState.Deleted);
                    }
                }
                ctx.SaveChanges();
            }
        }

        #endregion
    }
}
